/**
 * Created by Administrator on 2018/11/5.
 */

/**
 * 添加方法
 */
function add_ajax(){
    $("#add_submit").attr("disabled","disabled");
    var ajax_url = "addCustomerAction";
    var query = $('#add_form').serialize();
    query =query +"&"+"add_flag_str="+$("#add_flag_str").val();
    $.ajax({
        url:ajax_url,
        data:query,
        type:"Post",
        dataType:"json",
        success:function(data){
            switch(data.status){
                case 200:
                    $("#add_window").modal("hide");
                    system_right_tip_set("",data.message,1);
                    $('#one_table').DataTable().draw();
                    break;
                case 404:
                    system_tip_set(data.message);
                    break;
            }
            $("#add_submit").removeAttr("disabled");
        }
    });
}

/**
 * 获取数据方法
 */
function get_data_ajax(data_id){
    $('#edit_flag_str').chosen({width:'100%'});
    var ajaxurl = "getCustomerData";
    var query = new Object();
    query.data_id = data_id;
    $.ajax({
        url:ajaxurl,
        data:query,
        type:"Post",
        dataType:"json",
        success:function(data){
            if(data.status == 200){
                $("#edit_name").val(data.content['name']);
                $("#edit_phone").val(data.content['phone']);
                $("#edit_company_name").val(data.content['company_name']);
                $("#edit_company_address").val(data.content['company_address']);

                var edit_flag_str = $('#edit_flag_str');
                $('#edit_flag_str option').prop("selected",false);
                if(data.content['flag_str'] != ""){
                    for(var i=0;i<data.content['flag_array'].length;i++){
                        edit_flag_str.find("option[value='"+data.content['flag_array'][i]+"']").prop("selected","selected");
                    }
                }
                edit_flag_str.trigger("chosen:updated");

                $("input[name='data_id']").val(data_id);

                $("#edit_window").modal("show");
            }else{
                system_tip_set(data.message);
            }
        }
    });
}

function edit_ajax(){
    $("#edit_submit").attr("disabled","disabled");
    var ajaxurl = "editCustomerAction";
    var query = $('#edit_form').serialize();
    query =query +"&"+"edit_flag_str="+$("#edit_flag_str").val();
    $.ajax({
        url:ajaxurl,
        data:query,
        type:"Post",
        dataType:"json",
        success:function(data){
            switch(data.status){
                case 200:
                    $("#edit_window").modal("hide");
                    system_right_tip_set("",data.message,1);
                    $('#one_table').DataTable().draw();
                    break;
                case 404:
                    system_tip_set(data.message);
                    break;
            }
            $("#edit_submit").removeAttr("disabled");
        }
    });
}

//批量删除
function delete_ajax() {
    $("#delete_submit").attr("disabled","disabled");

    var id_str = "";
    var asd = $("input:checkbox[name='check_box[]']:checked").each(function(index, element) {
        if(id_str == ""){
            id_str = $(this).val();
        }else{
            id_str += "," + $(this).val();
        }
    });

    if(id_str == ""){
        $("#delete_submit").removeAttr("disabled");
        system_tip_set("请选择要删除的数据！");
        return false;
    }

    var ajaxurl = "deleteCustomerAction";
    var query = new Object();
    query.id_str = id_str;
    $.ajax({
        url:ajaxurl,
        data:query,
        type:"Post",
        dataType:"json",
        success:function(data){
            switch(data.status){
                case 200:
                    $("#delete_window").modal("hide");
                    system_right_tip_set("",data.message,1);
                    $('#one_table').DataTable().draw();
                    break;
                case 404:
                    system_tip_set(data.message);
                    break;
            }
            $("#delete_submit").removeAttr("disabled");
        }
    });
}

$(window).on('load', function() {
    $('#add_flag_str').chosen({width:'100%'});

    var data_table_init_1 = data_table_init;
    data_table_init_1.ajax = {
        url: 'customerQuery',
        dataSrc: 'data',
    };
    data_table_init_1.columnDefs = [{
        "targets": [0,1,2,3,5,7],
        "orderable": false
    }];
    data_table_init_1.columns = [
        {"data": "checkbox"},
        {"data": "name"},
        {"data": "phone"},
        {"data": "company_name"},
        {"data": "company_address"},
        {"data": "flag_str"},
        {"data": "add_time"},
        {"data": "action"}
    ];
    data_table_init_1.order = [6, 'desc'];
    var one_table = $('#one_table').DataTable(data_table_init_1);

    $('#table_list_toolbar').appendTo($("div.toolbar"));

    $('#search_name').on( 'change', function () {
        one_table
            .columns( 0 )
            .search( this.value )
            .draw();
    });
    $('#search_flag').on( 'change', function () {
        one_table
            .columns( 1 )
            .search( this.value )
            .draw();
    });
});

// 输入验证
$().ready(function() {
    var e = "<i class='fa fa-times-circle'></i> ";
    //添加管理员
    $("#add_form").validate({
        rules: {
            add_name: {
                required: !0
            }
        },
        messages: {
            add_name: {
                required: e + "请输入客户姓名"
            }
        },
        submitHandler:function(form){
            add_ajax();
        }
    });

    //编辑管理员
    $("#edit_form").validate({
        rules: {
            edit_name: {
                required: !0
            }
        },
        messages: {
            edit_name: {
                required: e + "请输入客户姓名"
            }
        },
        submitHandler:function(form){
            edit_ajax();
        }
    });
});